<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESSystemInstallerModeAJAX extends ESSystemInstaller
{   
    function loginPrompt()
    {
        if (!$this->checkInstallSession()) {
            $this->AJAXResponse->insertScript("showFloat();");
            $this->AJAXResponse->insertScript("showFloatingContent('Admin Log In', 'installauthbox');");
        } else {
            $this->AJAXResponse->insertScript("showFloat();");
            $this->AJAXResponse->insertScript("getAction();");
        }
        return $this->AJAXResponse;
    }
    
    function installAuth($input)
    {
	    global $localSettings;
        $input = get_object_vars($input);
        $tempMessage = '';
        if ($input['admin_name'] != $localSettings['admin']) {
            $tempMessage.= "\n - ".sprintf($GLOBALS['ES']['LANG']['AUTH']['FAIL']['INVALID'], 'username');
        }
        if ($input['admin_password'] != $localSettings['admin_pass']) {
            $tempMessage.= "\n - ".sprintf($GLOBALS['ES']['LANG']['AUTH']['FAIL']['INVALID'], 'password');
        }
        if ($input['admin_name'] == $localSettings['admin'] && $input['admin_password'] == $localSettings['admin_pass']) {
            $this->createInstallSession($input);
            $this->AJAXResponse->insertScript("hideFloatingContent('".strtolower(__FUNCTION__)."box');");
            $this->AJAXResponse->insertScript("getAction();");
            return $this->AJAXResponse;
        }
        if ($tempMessage != '') {
            $message = "Invalid information entered.";
            $message.= $tempMessage;
            $message.= "\nPlease correct these fields.";
            $this->AJAXResponse->insertAlert($message);
            return $this->AJAXResponse;
        }
    }
    
    function configCreate($input)
    {
        $input = get_object_vars($input);
        if ($this->installConfigFile($input)) {
            $this->AJAXResponse->insertScript("hideFloatingContent('".strtolower(__FUNCTION__)."box');");
            $this->AJAXResponse->insertScript("getAction();");
        } else {
            $this->AJAXResponse->insertAlert($GLOBALS['ES']['LANG']['INSTALLFAIL']['NOCONFIGCREATE']);
        }
        return $this->AJAXResponse;
    }
    
    function DBCreate($input)
    {
        $input = get_object_vars($input);
        $return = $this->installDB($input);
        if (!$return['SUCCESS']) {
            $msg = ($return['MODE'] == '1') ? 'The database info has been updated, but still unable to connect to it successfully.' : 'The database could not be created. Please try to manually create it.';
            $this->AJAXResponse->insertAlert($msg);
        } else {
            $msg = ($return['MODE'] == '1') ? 'The database info has been updated and database connection successful.' : 'The database has been created successfully.';
            $this->AJAXResponse->insertAlert($msg);
            $this->AJAXResponse->insertScript("hideFloatingContent('".strtolower(__FUNCTION__)."box');");
            $this->AJAXResponse->insertScript("getAction();");
        }
        return $this->AJAXResponse;
    }
    
    function tablesCreate()
    {
        $message = $this->installTables();
        $this->AJAXResponse->insertAlert($message);
        $this->AJAXResponse->insertScript("hideFloatingContent('".strtolower(__FUNCTION__)."box');");
        $this->AJAXResponse->insertScript("getAction();");
        return $this->AJAXResponse;
    }
    
    function htaccessCreate()
    {
        $return = $this->installHtaccess();
        $message = (!$return['SUCCESS']) ? 'The .htaccess file could not be created. Please check the manual and generate the same manually.' : 'The .htaccess file was successully installed.';
        $this->AJAXResponse->insertAlert($message);
        $this->AJAXResponse->insertScript("hideFloatingContent('".strtolower(__FUNCTION__)."box');");
        $this->AJAXResponse->insertScript("getAction();");
        return $this->AJAXResponse;
    }
    
    function installLockCreate()
    {
        $return = $this->installLock();
        if (!$return['SUCCESS']) {
            $this->AJAXResponse->insertAlert('The process terminated abruptly. Please run the installer again.');
        } else {
            $this->AJAXResponse->insertScript("hideFloat();");
            $this->AJAXResponse->insertAlert("Congratulations!\nThe installation process has been successfully completed.");
            $this->AJAXResponse->insertScript("window.location = '".ES_BASE_URL."';");
        }
        return $this->AJAXResponse;
    }
    
    function getAction()
    {
        $currentStepData = $this->getCurrentInstallStep();
        if ($currentStepData['FINALSTEP']) {
            $this->installLockCreate();
        } else {
            $this->AJAXResponse->insertScript("showFloatingContent('".$currentStepData['STEPNAME']."', '".$currentStepData['CONTENTS']."box');");
        }
        return $this->AJAXResponse;
    }
}

?>